package com.libinshen.wxpay.videoAliBaBa.controller;

import com.libinshen.wxpay.common.enums.BaseResponseCodeEnum;
import com.libinshen.wxpay.common.exception.BaseException;
import com.libinshen.wxpay.common.vo.CommonResult;
import com.libinshen.wxpay.videoAliBaBa.entity.Chapter;
import com.libinshen.wxpay.videoAliBaBa.entity.vo.ChapterVo;
import com.libinshen.wxpay.videoAliBaBa.service.ChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lbs
 */
@CrossOrigin
@Api("课程管理")
@RequestMapping("/edu/chapter")
@Slf4j
public class ChapterController {

    @Resource
    private ChapterService chapterService;

    @ApiOperation(value = "嵌套章节数据列表")
    @GetMapping("nested-list/{courseId}")
    public CommonResult nestedListByCourseId(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId) {
        if (StringUtils.isEmpty(courseId)) {
            throw new BaseException(BaseResponseCodeEnum.DATA_NOT_FOUND.getCode(), "id不能为空");
        }

        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return CommonResult.ok().data("items", chapterVoList);
    }

    @ApiOperation(value = "新增章节")
    @PostMapping
    public CommonResult save(
            @ApiParam(name = "chapterVo", value = "章节对象", required = true)
            @RequestBody Chapter chapter) {

        chapterService.save(chapter);
        return CommonResult.ok();
    }

    @ApiOperation(value = "根据ID查询章节")
    @GetMapping("get/{id}")
    public CommonResult getById(
            @ApiParam(name = "id", value = "章节ID", required = true)
            @PathVariable String id) {
        if (StringUtils.isEmpty(id)) {
            throw new BaseException(BaseResponseCodeEnum.DATA_NOT_FOUND.getCode(), "id不能为空");
        }

        Chapter chapter = chapterService.getById(id);
        if (chapter == null) {
            throw new BaseException(BaseResponseCodeEnum.DATA_NOT_FOUND.getCode(), "数据不存在");
        }
        return CommonResult.ok().data("item", chapter);
    }

    @ApiOperation(value = "根据ID修改章节")
    @PutMapping("update")
    public CommonResult updateById(
            @ApiParam(name = "chapter", value = "章节对象", required = true)
            @RequestBody Chapter chapter) {

        boolean isUpdated = chapterService.updateById(chapter);
        if (!isUpdated) {
            return CommonResult.ok().setMessage("数据不存在");
        }
        return CommonResult.ok().setMessage("数据修改成功");
    }

    @ApiOperation(value = "根据ID删除章节")
    @DeleteMapping("remove/{id}")
    public CommonResult removeById(
            @ApiParam(name = "id", value = "章节ID", required = true)
            @PathVariable String id) {
        if (StringUtils.isEmpty(id)) {
            return CommonResult.error().setMessage("id不能为空");
        }

        boolean result = chapterService.removeChapterById(id);
        if (!result) {
            return CommonResult.error().setMessage("删除失败");
        }
        return CommonResult.ok();
    }
}
